package com.zara.store.client.clientmodel;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Logger;

import com.thoughtworks.xstream.XStream;
import com.zara.store.client.model.StoreProxy;
import com.zara.store.common.PalcItemVO;
import com.zara.store.common.PalcItemViewVO;
import com.zara.store.common.PalcVO;
import com.zara.store.common.ProductVO;

import framework.clientmodel.Model;

public class PalcModel extends Model {
	
	private StoreProxy sp;
	private static Logger LOG = Logger.getLogger(PalcModel.class.getName());
	
	public PalcModel(StoreProxy sp) {
		this.sp = sp;
	}

	public void queryPalcProductList() {
		Collection<PalcItemViewVO> productList = sp.queryPalcProductList();
		for(PalcItemViewVO i: productList) {
			items.add(new PalcItemModel(i.getProduct(), i.getAmountSoldLastWeek(), i.getAmountPendingOnLastPalc()));
		}
		notifyViews();
	}

	public void queryAndAddProduct(String barcode) {
		if(!hasProduct(barcode)) {
			ProductVO product = sp.queryProduct(barcode);
			if(product != null) {
				items.add(new PalcItemModel(product));
				notifyViews();
			}
		}
	}
	
	public void changeProductAmount(int pos, BigDecimal amount) {
		items.get(pos).setAmount(amount);
	}
	
	public List<PalcItemModel> getItems() {
		return items;
	}
	
	public int getLastIndex() {
		return items.size()-1;
	}
	
	public ProductVO getLastProduct() {
		if(items.isEmpty())
			return null;
		else 
			return items.get(getLastIndex()).getProduct();
	}
	
	List<PalcItemModel> items = new ArrayList<PalcItemModel>();
	private boolean palcCreado = false;
	
	private boolean hasProduct(String barcode) {
		for(PalcItemModel e: items) {
			if(barcode.equals(e.getProduct().getBarcode())) {
				return true;
			}
		}
		return false;
	}

	public int getSize() {
		return items.size();
	}

	public void clearAll() {
		items = new ArrayList<PalcItemModel>();
		palcCreado = false;
		notifyViews();
	}

	public boolean getPalcCreado() {
		return palcCreado;
	}

	public void createPalc(File file) throws IOException {
		List<PalcItemVO> order = new ArrayList<PalcItemVO>();

		LOG.info(file.getAbsolutePath());

		for(PalcItemModel i: items) {
			BigDecimal amount = i.getAmount();
			if(!BigDecimal.ZERO.equals(amount)) {
				order.add(new PalcItemVO(i.getProduct().getId(), amount));
			}
		}

		if(order.size()>0) {
			PalcVO palcVO = sp.createPalc(order);
			XStream xstream = new XStream();
			xstream.addDefaultImplementation(ArrayList.class, Collection.class);
			xstream.addDefaultImplementation(java.sql.Date.class, java.util.Date.class);
			xstream.alias("palc", PalcVO.class);
			xstream.alias("product", PalcItemVO.class);
			FileOutputStream os = null;
			try {
				os = new FileOutputStream(file);
				xstream.toXML(palcVO, os);
			} finally {
				if(os != null) {
					os.close();
				}
			}
			palcCreado = true;
			notifyViews();
		}
	}

}
